package br.usp.ime.origami.foldcut.structures;

public class Pair<T>
{
	private T first, second;

	public Pair(T first, T second)
	{
		this.first = first;
		this.second = second;
	}

	public T getFirst()
	{
		return first;
	}

	public T getSecond()
	{
		return second;
	}

	@Override
	public boolean equals(Object obj)
	{
		Pair<T> p = (Pair<T>) obj;

		if (p.first != null && p.second != null)
		{
			return (p.first.equals(first) && p.second.equals(second)) || (p.first.equals(second) && p.second.equals(first));
		}
		else if (p.first != null)
		{
			return (p.first.equals(first) && second == null) || (p.first.equals(second) && first == null);
		}
		else if (p.second != null)
		{
			return (p.second.equals(first) && second == null) || (p.second.equals(second) && first == null);
		}
		else
		{
			return (first == null && second == null);
		}
	}

	@Override
	public int hashCode()
	{
		if (first != null && second != null)
		{
			return first.hashCode() * second.hashCode();
		}
		else if (first != null)
		{
			return first.hashCode();
		}
		else if (second != null)
		{
			return second.hashCode();
		}
		else
		{
			return 0;
		}

	}

	@Override
	public String toString()
	{

		return String.format("[%s,%s]", first, second);
	}

	public boolean contains(T o)
	{
		return first.equals(o) || second.equals(o);
	}
}
